package com.example.administrator.daydayappmarket.recommend.fragment;


import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.example.administrator.daydayappmarket.Contacts;
import com.example.administrator.daydayappmarket.R;
import com.example.administrator.daydayappmarket.recommend.adapter.RecommendMainFootListViewAdapter;
import com.example.administrator.daydayappmarket.recommend.moduls.RecommendApps;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

/**
 * A simple {@link Fragment} subclass.
 */
public class RecommendTopSecondFragment extends Fragment {

    private PullToRefreshListView listView;

    private RecommendMainFootListViewAdapter adapter;

    private List<RecommendApps> appses;

    private MyReceiver receiver;
    private String s;
    private RequestQueue requestQueue;
    private StringRequest request;



    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        appses = new ArrayList<>();
        requestQueue = Volley.newRequestQueue(getContext());
        adapter = new RecommendMainFootListViewAdapter(appses,getContext());

        IntentFilter filter = new IntentFilter("com.appMarket.recommend.top.second.action.first");
        receiver = new MyReceiver();
        getActivity().registerReceiver(receiver,filter);

    }

    class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            ArrayList<String> ids = intent.getStringArrayListExtra("ids");
            s = ids.get(1);
            stringrequest(null);

        }
    }

    private void stringrequest(final PullToRefreshBase<ListView> refreshView) {
        request = new StringRequest(Contacts.TAB_TOPICFRAGMENT+s
                , new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    appses.clear();
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        JSONObject data = jsonObject.getJSONObject("data");
                        RecommendApps apps = new RecommendApps();
                        apps.JsonParse(data);
                        appses.add(apps);
                    }
                    adapter.notifyDataSetChanged();
                    if (refreshView!=null){
                        refreshView.onRefreshComplete();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        request.setTag("topfristrequest");
        requestQueue.add(request);
        requestQueue.start();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment

        View inflate = inflater.inflate(R.layout.fragment_recommend_top_first, container, false);
        View view = LayoutInflater.from(getContext()).inflate(R.layout.layout_listview_empty,null);
        ImageView imageView = (ImageView) view.findViewById(R.id.layout_listView_empty_image);
        Glide.with(getContext()).load(R.drawable.cat_loading_transparent).into(imageView);
        listView = (PullToRefreshListView) inflate.findViewById(R.id.fragment_recommend_top_frist_pullToRefreshListView);

        listView.setAdapter(adapter);
        listView.setEmptyView(view);
        listView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(final PullToRefreshBase<ListView> refreshView) {

                stringrequest(refreshView);

            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
        return inflate;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        requestQueue.cancelAll("topfristrequest");
        getActivity().unregisterReceiver(receiver);
    }

}
